package List;

import java.util.Arrays;
import List.*;

/**
 * @Author liangzai
 * @Description:
 */
public class MyArrList implements IList{
    public static final int CAPACITY = 10;
    public int[] arr;
    private int useSize;

    public MyArrList() {
        this.arr = new int[CAPACITY];
    }

    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }

        arr[useSize] = data;

        useSize++;
    }

    private void grow(){
        this.arr = Arrays.copyOf(this.arr,2*this.arr.length);
    }

    @Override
    public boolean isFull() {
        if(this.arr.length==useSize){
            return true;
        }else {
            return false;
        }
    }

    private void checkpos(int pos) throws PosException{
        if(pos<0 || pos>useSize){
            throw new PosException();
        }

    }

    @Override
    public void add(int pos, int data) {
        try {
            checkpos(pos);
            if (isFull()) {
                grow();
            }

            for (int i = useSize - 1; i >= pos; i--) {
                this.arr[i + 1] = arr[i];
            }
            arr[pos] = data;
            useSize++;
        }catch (PosException e){
            System.out.println("pos位置不合法！");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(arr[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(arr[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    private void checkpos2(int pos){
        if(pos<0 || pos>=useSize){
            throw new PosException();
        }
    }

    @Override
    public int get(int pos) {
        try{
            checkpos2(pos);
            return this.arr[pos];
        }catch(PosException e){
            System.out.println("pos位置不合法！");
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkpos2(pos);
            checkList();
            arr[pos] = value;
        }catch(PosException e){
            System.out.println("pos位置不合法！");
            e.printStackTrace();
        }catch (ListException e){
            System.out.println("顺序表为空！");
            e.printStackTrace();
        }
    }

    private void checkList(){
        if(this.useSize==0){
            throw new ListException("顺序表为空！");
        }
    }


    @Override
    public void remove(int toRemove) {
        try {
            checkList();
            int pos = indexOf(toRemove);
            if (pos == -1) {
                System.out.println("此元素不存在！");
                return;
            }
            if (pos == arr.length - 1) {
                arr[pos] = 0;
            } else {
                for (int i = pos; i < useSize - 1; i++) {
                    arr[i] = arr[i + 1];
                }
                arr[useSize - 1] = 0;
            }
            useSize--;
        }catch (ListException e){
            System.out.println("顺序表为空！");
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {
        if(useSize==0){
            return ;
        }
        for (int i = useSize-1; i >= 0 ; i--) {
            arr[i] = 0;
        }
        useSize=0;
    }

    @Override
    public void display() {
        for (int i = 0; i < useSize; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
}
